/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sistema.entidades;

import br.com.sistema.entidades.editor.Editor;
import br.com.sistema.excecoes.EntidadeNaoEncontrada;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;

/**
 *
 * @author Lauro
 */
public class Destaque extends Noticia{
    private String foto;

    public Destaque(int id) throws SQLException, EntidadeNaoEncontrada {
        super(id);
        buscar();
    }
    
    //<editor-fold defaultstate="collapsed" desc="Metodos Getters & Setters.">
    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Strings SQL.">
    private String getSQLInsercao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO destaque ( ");
        sql.append("   id, ");
        sql.append("   foto ");
        sql.append(" ) VALUES ( ");
        sql.append("   ?, ");
        sql.append("   ? ");
        sql.append(" ) ");
        return sql.toString();
    }
    
    private String getSQLAtualizacao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE ");
        sql.append(" destaque ");
        sql.append(" SET ");
        sql.append(" foto=? ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }
    
    private String getSQLExclusao() {
        StringBuilder sql = new StringBuilder();
        sql.append(" DELETE FROM ");
        sql.append(" destaque ");
        sql.append(" WHERE id = ? ");
        return sql.toString();
    }
    
    @Override
    protected String getSQLBuscarPorId() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT destaque.*, pagina.*, noticia.*, categoria.* ");
        sql.append(" FROM noticia ");
        sql.append(" INNER JOIN noticia ON noticia.id = destaque.id ");
        sql.append(" INNER JOIN pagina ON pagina.id = noticia.id ");
        sql.append(" LEFT OUTER JOIN categoria ON categoria.id = noticia.categoria ");
        sql.append(" WHERE noticia.id = ? ");
        return sql.toString();
    }
    // </editor-fold>
    
    // BEGIN: CRUD
    //<editor-fold defaultstate="expanded" desc="Metodos CRUD.">
    @Override
    public void buscar() throws SQLException, EntidadeNaoEncontrada {
        getConexao();
        PreparedStatement query = getConexao().prepareStatement(getSQLBuscarPorId());
        query.setInt(1, this.getId()); // id
        ResultSet result = query.executeQuery();
        if (result.next()) {
            this.setId(result.getInt("destaque.id"));
            this.setTitulo(result.getString("pagina.titulo"));
            this.setConteudo(result.getString("pagina.conteudo"));
            this.setCategoria(Categoria.getCategoria(result.getInt("categoria.id")));
            this.setData(new Date(result.getTimestamp("noticia.data").getTime()));
            this.setFoto(result.getString("destaque.foto"));
        }else{
            throw new EntidadeNaoEncontrada();
        }
    }

    @Override
    protected void inserir() throws SQLException {
        super.salvar();
        PreparedStatement query = getConexao().prepareStatement(getSQLInsercao(), Statement.RETURN_GENERATED_KEYS);
        query.setInt(1, this.getId());
        query.setString(2, this.getFoto());
        query.executeUpdate();
        ResultSet result = query.getGeneratedKeys();
        result.first();
        int result_id = result.getInt(1);
        this.setId(result_id);
    }

    @Override
    protected void atualizar() throws SQLException {
        PreparedStatement query = getConexao().prepareStatement(getSQLAtualizacao());
        query.setString(1, this.getFoto());
        query.setInt(2, this.getId()); // id
        query.executeUpdate();
    }
    
    @Override
    protected void excluir() throws SQLException{
        PreparedStatement query = getConexao().prepareStatement(getSQLExclusao());
        query.setInt(1, this.getId()); // id
        query.executeUpdate();
    }
    // </editor-fold>
    // END: CRUD
}
